<template>
  <div>
    <el-row>
      <el-col :span="5"></el-col>
      <el-col :span="15" style="margin-top: 5vh">
        <el-table :data="userList" style="width: 100%">
          <el-table-column type="index" :index="index" width="180" />
          <el-table-column prop="userName" label="Name" width="180" />
          <el-table-column prop="email" label="Email" width="180" />
          <el-table-column prop="role" label="Role" width="180" />
          <el-table-column label="操作" min-width="180" header-align="center" align="center">
            <template #default="props">
              <el-button :type=" props.row.role === 'USER' ? 'primary' : 'warning' " @click="setUser(props.row.id, props.row.role)" >{{ props.row.role === "USER" ? '设为管理员' : '撤销管理员' }}</el-button >
              <el-button type="danger" @click="deleteUser(props.row.id)">删除用户</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          style="margin-left: 20vw; margin-top: 2vh;"
          :currentPage="page.currentPage"
          :page-size="page.pageSize"
          layout="prev, pager, next"
          :total="page.num"
          @current-change="handleCurrentChange"
        />
      </el-col>
    </el-row>
  </div>
</template>

<script>
import api from '../../api/data.js'
import { ElMessage } from 'element-plus'
export default {
  name: 'AdminUser',
  components: {

  },
  data () {
    return {
      userList: [],
      page: {
        pageSize: 10,
        currentPage: 1,
        num: 1,
      }
  }},
  methods: {
    setUser (id, role) {
      api.updateUserRole({
        token: this.$store.state.token,
        role: role === 'USER' ? 'ADMIN' : 'USER',
        userId: id
      }).then((res) => {
        if (res.code === 342) {
          this.$store.commit('logout')
          this.$store.commit('tokenError')
          this.$router.push({path: '/login'})
        } else if (res.code < 300) {
          this.pageChange()
        } else {
          ElMessage.error(res.message)
        }
      })
    },
    deleteUser (id) {
      api.deleteUser({
        token: this.$store.state.token,
        userId: id
      }).then((res) => {
        if (res.code === 342) {
          this.$store.commit('logout')
          this.$store.commit('tokenError')
          this.$router.push({path: '/login'})
        } else if (res.code < 300) {
          this.pageChange()
        } else {
          ElMessage.error(res.message)
        }
      })
    },
    handleCurrentChange (val) {
      this.page.currentPage = val
      this.pageChange()
      // console.log(`current page: ${val}`)
    },
    pageChange () {
      api.getAllUsers({
        token: this.$store.state.token,
        pageSize: this.page.pageSize,
        currentPage: this.page.currentPage
      }).then((res) => {
        if (res.code === 342) {
          this.$store.commit('logout')
          this.$store.commit('tokenError')
          this.$router.push({path: '/login'})
        } else if (res.code < 300) {
          this.userList = res.data.userList
          this.page.pageSize = res.data.pageSize
          this.page.currentPage = res.data.currentPage
          this.page.num = res.data.pageNum * res.data.pageSize
          // console.log(this.page)
        } else {
          ElMessage.error(res.message)
        }
      })
    }
  },
  mounted () {
    this.pageChange()
  }
}
</script>

<style scoped>

</style>
